import {createRouter, createWebHistory} from "vue-router";
import Index from "../view/Index.vue";
import Home from "../view/user/home/Home.vue"
import Login from "../view/login/Login.vue";
import Registry from "../view/login/Registry.vue";
import BackPassword from "../view/login/BackPassword.vue";
import StudentIndex from "../view/user/student/Index.vue";
import StudentProfile from "../view/user/student/Profile.vue";
import StudentHonors from "../view/user/student/Honors.vue";
import StudentGrades from "../view/user/student/Grades.vue";
import StudentEvaluation from "../view/user/student/Evaluation.vue"
import StudentActivities from "../view/user/student/Activities.vue";
import FeedbackIndex from "../view/user/feedback/Index.vue";
import Feedback from "../view/user/feedback/Feedback.vue";
import AdminStudentIndex from "../view/admin/student/Index.vue";
import AdminStudentList from "../view/admin/student/List.vue";
import AdminStudentEdit from "../view/admin/student/Edit.vue";
import AdminGradeInfo from "../view/admin/grade/Info.vue";
import AdminGradeView from "../view/admin/grade/View.vue";
import AdminGradeEdit from "../view/admin/grade/Edit.vue";
import AdminActivityInfo from "../view/admin/activity/Info.vue";
import AdminActivityView from "../view/admin/activity/View.vue";
import AdminActivityEdit from "../view/admin/activity/Edit.vue";
import AdminHonorInfo from "../view/admin/honor/Info.vue";
import AdminHonorApprove from "../view/admin/honor/Approve.vue";
import AdminEvaluationInfo from "../view/admin/evaluation/Info.vue";
import AdminEvaluationCriteria from "../view/admin/evaluation/Criteria.vue";
import AdminEvaluationStats from "../view/admin/evaluation/Stats.vue";
import AdminLayout from "../view/admin/Index.vue";
import {ElMessage} from "element-plus";

// 修改管理员模块的组件导入路径

// 子页面组件

//meta元数据 title用于面包屑展示。
const routes = [
    {//一进来登录
        path: '/',
        redirect: '/login'
    },
    {//登录页
        path: '/login',
        name: 'login',
        component: Login,
    },
    {//注册页
        path: '/registry',
        name: 'registry',
        component: Registry,
    },
    {//找回密码
        path: '/backPassword',
        name: 'backPassword',
        component: BackPassword,
    },
    {//主体框架
        path: '/index',
        name: 'index',
        component: Index,
        meta: {title: '首页'},
        redirect: '/home', //默认组件展示，切到index组件的时候，就去显示地址为/home的组件
        children: [ //子路由 点击左侧导航栏跳转到指定子路由
            {//首页
                path: '/home',
                component: Home,
                name: 'home',
                meta: {title: '首页'},
            },
            //学生管理
            {
                path: "/studentIndex",
                component: StudentIndex,
                name: "studentIndex",
                meta: {title: '学生管理'},
                redirect: "/studentIndex/profile",
                children: [
                    // 新增学生通块路由
                    {
                        path: '/studentIndex/profile',
                        component: StudentProfile,
                        name: 'studentProfile',
                        meta: {title: '个人信息展示'}
                    },
                    {
                        path: '/studentIndex/grades',
                        component: StudentGrades,
                        name: 'studentGrades',
                        meta: {title: '成绩查询'}
                    },
                    {
                        path: '/studentIndex/activities',
                        component: StudentActivities,
                        name: 'studentActivities',
                        meta: {title: '活动记录表'}
                    },
                    {
                        path: '/studentIndex/honors',
                        component: StudentHonors,
                        name: 'studentHonors',
                        meta: {title: '荣誉申请表'}
                    },
                    {
                        path: '/studentIndex/evaluation',
                        component: StudentEvaluation,
                        name: 'studentEvaluation',
                        meta: {title: '测评结果查看'}
                    }
                ]
            },
            // 新增信息反馈模块路由
            {
                path: "/feedbackIndex",
                component: FeedbackIndex,
                name: "feedbackIndex",
                meta: {title: '信息反馈中心'},
                redirect: "/feedbackIndex/feedback",
                children: [
                    {
                        path: '/feedbackIndex/feedback',
                        component: Feedback,
                        name: 'feedback',
                        meta: {title: '问题反馈'}
                    }
                ]
            }
        ]
    },
    // 添加管理员模块路由
    {
        path: '/admin',
        component: AdminLayout,
        meta: { requiresAdmin: true },
        redirect: '/admin/student/list',
        children: [
            {
                path: '/admin/student',
                component: AdminStudentIndex,
                name: 'studentList',
                meta: { title: '学生管理' },
                children: [
                    {
                        path: '/admin/student/list',
                        component: AdminStudentList,
                        name: 'studentList',
                        meta: { title: '学生列表' }
                    },
                    {
                        path: '/admin/student/edit/:id?',
                        component: AdminStudentEdit,
                        name: 'studentEdit',
                        meta: { title: '编辑学生' }
                    }
                ]
            },
            {
                path: '/admin/grade',
                component: AdminGradeInfo,
                meta: { title: '成绩录入管理' },
                children: [
                    {
                        path: '/admin/grade/list',
                        component: AdminGradeView,
                        name: 'gradeList',
                        meta: { title: '成绩列表' }
                    },
                    {
                        path: '/admin/grade/edit/:id?',
                        component: AdminGradeEdit,
                        name: 'gradeEdit',
                        meta: { title: '编辑成绩' }
                    }
                ]
            },
            {
                path: '/admin/activity',
                component: AdminActivityInfo,
                meta: { title: '活动管理' },
                children: [
                    {
                        path: '/admin/activity/list',
                        component: AdminActivityView,
                        name: 'activityList',
                        meta: { title: '活动列表' }
                    },
                    {
                        path: '/admin/activity/edit/:id?',
                        component: AdminActivityEdit,
                        name: 'activityEdit',
                        meta: { title: '编辑活动' }
                    }
                ]
            },
            {
                path: '/admin/honor',
                component: AdminHonorInfo,
                meta: {title: '荣誉榜'},
                children: [
                    {
                        path: '/admin/honor/index',
                        component: AdminHonorApprove,
                        name: 'honorApprove',
                        meta: { title: '荣誉审核' }
                    }
                ]
            },
            {
                path: '/admin/evaluation',
                component: AdminEvaluationInfo,
                children: [
                    {
                        path: '/admin/evaluation/criteria',
                        component: AdminEvaluationCriteria,
                        name: 'evaluationCriteria',
                        meta: { title: '评分标准' }
                    },
                    {
                        path: '/admin/evaluation/stats',
                        component: AdminEvaluationStats,
                        name: 'evaluationStats',
                        meta: { title: '评分统计' }
                    }
                ]
            },
            // {//用户管理页路由
            //     path: "/userIndex",
            //     component: UserIndex,
            //     name: "userIndex",
            //     meta: {title: '用户中心'},
            //     redirect: "/userIndex/userList",
            //     children: [
            //         {
            //             //用户管理
            //             path: "/userIndex/userList",
            //             name: 'userList',
            //             component: UserList,
            //             meta: {title: '用户列表'},
            //         },
            //         {
            //             //用户展示页
            //             path: "/userIndex/userInfo",
            //             name: 'userInfo',
            //             component: UserInfo,
            //             meta: {title: '用户设置'},
            //         }
            //     ]
            // }
        ]
    }
]

const router = createRouter({
    history: createWebHistory(),
    routes,
});

// 添加全局路由守卫进行权限验证
router.beforeEach((to, from, next) => {
    const userRole = localStorage.getItem('userRole')

    // 检查是否需要管理员权限
    if (to.matched.some(record => record.meta.requiresAdmin)) {
        if (userRole === '1') {
            next()
        } else {
            ElMessage.error('无权限访问该页面')
            next('/home')
        }
    } else {
        next()
    }
})

export default router;
